﻿using System.Xml;
using UnityEngine;
using System.Collections;

public class ReadXml {

    public static ReadXml _instance = new ReadXml();

    private XmlDocument ReadAndLaodFood()
    {
        XmlDocument doc = new XmlDocument();

        TextAsset textAsset = (TextAsset)Resources.Load("Xml/foods");

        doc.LoadXml(textAsset.text);

        return doc;
    }

    private XmlDocument ReadAndLaodSkill()
    {
        XmlDocument doc = new XmlDocument();

        TextAsset textAsset = (TextAsset)Resources.Load("Xml/skill");

        doc.LoadXml(textAsset.text);

        return doc;
    }

    /// <summary>
    /// 传入ID来取食物数据
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public FoodInfo GetFoodById(int id)
    {
        FoodInfo info = new FoodInfo();

        XmlDocument xmlDoc = ReadAndLaodFood();

        XmlNodeList gods = xmlDoc.SelectNodes("Content/god");

        foreach (XmlNode god in gods)
        {
            if (id == int.Parse(god.Attributes["id"].Value))
            {
                info.ID = int.Parse(god.Attributes["id"].Value);
                info.FoodName = god.Attributes["godname"].Value;
                info.SpriteName = god.Attributes["spritename"].Value;
                info.Race = god.Attributes["race"].Value;
                info.AttackForce = int.Parse(god.Attributes["attackforce"].Value);
                info.Health = int.Parse(god.Attributes["health"].Value);
                info.Speed = int.Parse(god.Attributes["speed"].Value);

                string releaseRange = god.Attributes["normalattack"].Value;
                string[] tempReleaseRange = releaseRange.Split(',');
                info.AttackRange = new int[Mathf.Max(0, tempReleaseRange.Length)];
                for (int i = 0; i < tempReleaseRange.Length; i++)
                {
                    info.AttackRange[i] = int.Parse(tempReleaseRange[i]);
                    //Debug.Log(info.ID);
                }

                info.SkillID = int.Parse(god.Attributes["magicalpower"].Value);
            }
        }
        return info;
    }

    /// <summary>
    /// 得到所有食物的ID
    /// </summary>
    /// <returns></returns>
    public int[] GetAllFoodsID()
    {
        XmlDocument xmlDoc = ReadAndLaodFood();

        XmlNodeList foods = xmlDoc.SelectNodes("Content/god");

        int[] temp = new int[foods.Count];
        int i = 0;
        foreach (XmlNode god in foods)
        {
            temp[i] = int.Parse(god.Attributes["id"].Value);
            i++;
        }
        return temp;
    }

    /// <summary>
    /// 获得所有甜党的ID
    /// </summary>
    /// <returns></returns>
    public int[] GetAllSweetFoodsID()
    {
        XmlDocument xmlDoc = ReadAndLaodFood();

        XmlNodeList foods = xmlDoc.SelectNodes("Content/god");

        int[] temp = new int[foods.Count / 2];
        int i = 0;
        foreach (XmlNode god in foods)
        {
            if (god.Attributes["race"].Value == "甜党")
            {
                temp[i] = int.Parse(god.Attributes["id"].Value);
                //Debug.Log(temp[i]);
                i++;
            }
        }
        return temp;
    }

    /// <summary>
    /// 获得所有咸党的ID
    /// </summary>
    /// <returns></returns>
    public int[] GetAllSaltFoodsID()
    {
        XmlDocument xmlDoc = ReadAndLaodFood();

        XmlNodeList foods = xmlDoc.SelectNodes("Content/god");

        int[] temp = new int[foods.Count / 2];
        int i = 0;
        foreach (XmlNode god in foods)
        {
            if (god.Attributes["race"].Value == "咸党")
            {
                temp[i] = int.Parse(god.Attributes["id"].Value);
                i++;
            }
        }
        return temp;
    }


    /// <summary>
    /// 传入图片名称来获取食物
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public FoodInfo GetFoodBySpriteName(string name)
    {
        FoodInfo info = new FoodInfo();

        XmlDocument xmlDoc = ReadAndLaodFood();

        XmlNodeList gods = xmlDoc.SelectNodes("Content/god");

        foreach (XmlNode god in gods)
        {
            if (name == god.Attributes["spritename"].Value)
            {
                info.ID = int.Parse(god.Attributes["id"].Value);
                info.FoodName = god.Attributes["godname"].Value;
                info.SpriteName = god.Attributes["spritename"].Value;
                info.Race = god.Attributes["race"].Value;
                info.AttackForce = int.Parse(god.Attributes["attackforce"].Value);
                info.Health = int.Parse(god.Attributes["health"].Value);
                info.Speed = int.Parse(god.Attributes["speed"].Value);

                string releaseRange = god.Attributes["normalattack"].Value;
                string[] tempReleaseRange = releaseRange.Split(',');
                info.AttackRange = new int[Mathf.Max(0, tempReleaseRange.Length)];
                for (int i = 0; i < tempReleaseRange.Length; i++)
                {
                    info.AttackRange[i] = int.Parse(tempReleaseRange[i]);
                }

                info.SkillID = int.Parse(god.Attributes["magicalpower"].Value);
            }
        }
        return info;
    }


    /// <summary>
    /// 传入ID获得技能信息
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public SkillInfo GetSkillByID(int id)
    {
        SkillInfo info = new SkillInfo();

        XmlDocument xmlDoc = ReadAndLaodSkill();

        XmlNodeList skills = xmlDoc.SelectNodes("Content/skill");

        foreach (XmlNode skill in skills)
        {
            if (id == int.Parse(skill.Attributes["id"].Value))
            {
                info.id = int.Parse(skill.Attributes["id"].Value);
                info.skillname = skill.Attributes["skillname"].Value;
                info.change = int.Parse(skill.Attributes["chance"].Value);
                info.skilltype = SkillType.Attack;
                string s = skill.Attributes["skilltype"].Value;
                switch (s)
                {
                    case "Attack":
                        info.skilltype = SkillType.Attack;
                        break;
                    case "TakeDam":
                        info.skilltype = SkillType.TakeDam;
                        break;
                    case "Passive":
                        info.skilltype = SkillType.Passive;
                        break;
                    case "Stop":
                        info.skilltype = SkillType.Stop;
                        break;
                    case "Health":
                        info.skilltype = SkillType.Health;
                        break;
                    case "rage":
                        info.skilltype = SkillType.Rage;
                        break;
                    case "recover":
                        info.skilltype = SkillType.Recover;
                        break;
                    case "Control":
                        info.skilltype = SkillType.Control;
                        break;
                }
                info.value = int.Parse(skill.Attributes["value"].Value);
                info.energy = int.Parse(skill.Attributes["mana"].Value);
                info.des = skill.Attributes["des"].Value;
            }
        }

        return info;
    }
}
